<?php 
	jimport('joomla.application.component.model');
	class order_branchModelchuyen_hang extends JModel
	{
	   //cap nhat lai so luong de chuyen hang xuong kho
       function cap_nhat_so_luong($id_pro,$id_kho,$cons,$num,$id_bill)
       {
        $db = &JFactory::getDBO();
            $sql = "SELECT *
                    FROM #__erp_warehouse_ex_pro
                    WHERE id_product = '".$id_pro."'
                    AND id_warehouse = '".$_SESSION['id_kho']."'
                    AND consignment_code = '".$cons."'";
                    
            $db->setQuery($sql);
            //print_r($db);die;
            $rs =  $db->loadObject();      
            $num = $rs->number_of - $num;
            $sql = "UPDATE #__erp_warehouse_ex_pro SET
                                number_of = '".$num."'
                                WHERE id_product = '".$id_pro."'
                                AND id_warehouse = '".$_SESSION['id_kho']."'
                                AND consignment_code = '".$cons."'";
                                $db->setQuery($sql);
                                $db->query();  
            
            $sql = "UPDATE #__erp_warehouse_bill_io SET
                                status = 1
                                WHERE 
                                id ='".$id_bill."'";
                                $db->setQuery($sql);
                                $db->query();   
               
       }
	   //lay danh sach congsi theo id_product
       function lay_consig_theo_id_pro($id_pro)
       {
        $db = &JFactory::getDBO();
            $sql = "SELECT *
                    FROM #__erp_warehouse_ex_pro
                    WHERE id_product = '".$id_pro."'
                    AND id_warehouse = '".$_SESSION['id_kho']."'";
                    
            $db->setQuery($sql);
            //print_r($db);die;
            return $db->loadObjectList();         
       }
       
	   //danh sach san pham trong 1 bill
       function ds_sp_bill($id)
       {
        $db = &JFactory::getDBO();
            $sql = "SELECT pr.name as name_pro, pr.id as id_pro, ioe.number as number, ioe.id as id_ioe
                    FROM #__erp_warehouse_bill_io_extend ioe, #__erp_product pr
                    WHERE ioe.id_product = pr.id
                    AND ioe.id_bill = '".$id."' ";
                    
            $db->setQuery($sql);
            //print_r($db);die;
            return $db->loadObjectList();        
       }
       
	   //danh sach don nhan hang do o duoi goi len kho
	   function su_luoc_nhan_don_hang($date)
       {
           if($date == null)
            {
                $date = date('Y-m-d');
            }
            //dinh dang lai $date giong voi CSDL
            else
            {
                $d = substr($date,3,2);
                $m = substr($date,0,2);
                $y = substr($date,6,4);
                $date = $y."-".$m."-".$d;
                
            }  
            //echo $date;die;
            $db = &JFactory::getDBO();
            $sql = "SELECT * 
                    FROM #__erp_warehouse_bill_io
                    WHERE date_create LIKE '$date%'
                    AND type = 3";
                    
            $db->setQuery($sql);
            return $db->loadObjectList(); 
       }
	}